package com.goldenautumn.prefabricate.activity.mine.order

import android.os.Bundle
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.cart.SettlementActivity
import com.goldenautumn.prefabricate.activity.mine.adapter.AfterSalesAdapter
import com.goldenautumn.prefabricate.activity.mine.adapter.OrderItemAdapter
import com.goldenautumn.prefabricate.business.mall.GoodsInfoUiState
import com.goldenautumn.prefabricate.business.order.OrderListUiState
import com.goldenautumn.prefabricate.business.order.OrderViewModel
import com.goldenautumn.prefabricate.business.user.LoadingUiState
import com.goldenautumn.prefabricate.databinding.FragmentAfterSalesBinding
import com.goldenautumn.prefabricate.utils.Constants
import com.goldenautumn.prefabricate.utils.onCenterToast
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseFragment
import qncx.external.common.compat.gone
import qncx.external.common.compat.visible
import qncx.external.common.utils.intent
import qncx.external.common.widget.shapeview.shape.ShapeButton

class AfterSalesFragment : BaseFragment<FragmentAfterSalesBinding>() {

    companion object {
        fun newInstance(type: String): AfterSalesFragment {
            val fragment = AfterSalesFragment()
            val bundle = Bundle()
            bundle.putString(Constants.BUSINESS_PAGE_DATA, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    private val mAdapter by lazy {
        AfterSalesAdapter()
    }

    private val mOrderModel by viewModels<OrderViewModel>()

    private var page = 1

    private val orderType: String?
        get() = arguments?.getString(Constants.BUSINESS_PAGE_DATA)

    override fun getLayoutId(): Int = R.layout.fragment_after_sales

    override suspend fun initView() {
        mBinding.orderRv.layoutManager = LinearLayoutManager(mContext)
        mBinding.orderRv.adapter = mAdapter
        (mBinding.orderRv.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        mAdapter.setStateViewLayout(mContext,R.layout.recycler_empty_view)
        var msg = mAdapter.stateView?.findViewById<TextView>(R.id.prompt_content)
        var actionBtn = mAdapter.stateView?.findViewById<ShapeButton>(R.id.action_order)
        msg?.setText(R.string.not_order_msg)
        msg?.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, R.mipmap.ic_empty_order), null, null)
        actionBtn?.visible()
        actionBtn?.setOnClickListener {
            SPUtils.getInstance().put("home",0)
            activity?.onBackPressedDispatcher?.onBackPressed()
        }

        mAdapter.addOnItemChildClickListener(R.id.actionBtnRight) { adapter, _, position ->
            var item = adapter.getItem(position)
            startActivity(ApplyRefundDetailsActivity::class.intent.putExtra("ORDER",item?.order_refund_id))
        }

        var type = if(Constants.ORDER_TYPE_ALL == orderType) -1 else 0
        mOrderModel.onRefundList("$type")
        mBinding.refresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                mOrderModel.onRefundList("$type")
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mOrderModel.onRefundList("$type")
            }
        })

        while (isActive) {
            select {
                mOrderModel.loading.onReceive {
                    when (it) {
                        is LoadingUiState.LoadingUI -> {
                            if(!it.isShow) {
                                mBinding.refresh.finishRefresh()
                                mBinding.refresh.finishLoadMore()
                                mAdapter.isStateViewEnable = true
                            } else {}
                        }
                        else -> {}
                    }
                }
                mOrderModel.orderListRequests.onReceive {
                    when (it) {
                        is OrderListUiState.RefundList -> {
                            mAdapter.submitList(it.orderList)
                        }
                        else -> {}
                    }
                }
            }
        }

    }


}